<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="upload"
      :http-request="uploadImgs"
      :headers="headers"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl.url" :src="imageUrl.url" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-button type="primary" size="mini" @click="submit"
      >校验身份信息</el-button
    >
    <el-button type="danger" size="mini" @click="close">取消校验</el-button>
  </div>
</template>



<script>
import { getToken } from "@/utils/auth";
import { checkUserIdCard, uploadImgs } from "@/api/student/checkId";

export default {
  data() {
    return {
      imageUrl: {
        url: "",
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      date: {},
    };
  },
  methods: {
    uploadImgs(params) {
      const file = params.file;
      this.forms = new FormData();
      this.forms.append("file", file);
      uploadImgs(this.forms).then((response) => {
        this.$modal.msgSuccess("上传成功");
        this.imageUrl.url = response.data.url;
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl.url = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    submit() {
      checkUserIdCard(this.imageUrl).then((response) => {
          this.$modal.msgSuccess("认证成功");
      });
    },
    close(){
      this.imageUrl.url = ""
    }
  },
};
</script>
 

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>